//
//  MCMarketingDetailFooterView.swift
//  StoreManage
//
//  Created by MC on 2018/6/27.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCMarketingDetailFooterViewClosure = (MCChangeCommunityViewController) -> Void

class MCMarketingDetailFooterView: UIView {
    
    var model = MCCommunityModel() {
        didSet {
            if model.communityId.intValue == 0 {  // 没有配送社区
                addressButton.setTitle(" 还没有配送社区，快让店长去添加吧！", for: .normal)
                arrowImageView.isHidden = true
                addressButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.center
                addressButton.titleLabel?.font = MCFont(14)
            } else {
                nameLabel.text = model.communityName
                let address = " " + model.province + model.city + model.area + model.address
                addressButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
                addressButton.titleLabel?.font = MCFont(13)
                addressButton.setTitle(address, for: .normal)
                arrowImageView.isHidden = false
            }
        }
    }
    
    var closure : MCMarketingDetailFooterViewClosure?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(changeAddress))
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(tap)
        
        
        
        self.backgroundColor = UIColor.white
        self.frame = CGRect.init(x: 0, y: 0, width: MCScreenWidth, height: 130)
        
        self.addSubview(lineOneView)
        lineOneView.snp.remakeConstraints { (make) ->Void in
            make.left.top.right.equalTo(self)
            make.top.equalTo(15)
            make.height.equalTo(7)
        }
        
        
        self.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.top.equalTo(lineOneView.snp.bottom)
            make.height.equalTo(50)
        }
        
        self.addSubview(lineTwoView)
        lineTwoView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.top.equalTo(promptLabel.snp.bottom)
            make.height.equalTo(1)
        }
        
        self.addSubview(arrowImageView)
        arrowImageView.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.top.equalTo(lineTwoView.snp.bottom).offset(30)
            make.size.equalTo(CGSize.init(width: 8, height: 14))
        }

        
        self.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.top.equalTo(lineTwoView.snp.bottom).offset(10)
            make.height.equalTo(20)
            make.right.equalTo(arrowImageView.snp.left).offset(-5)
        }
        
        
        self.addSubview(addressButton)
        addressButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.top.equalTo(nameLabel.snp.bottom).offset(6)
            make.height.equalTo(18)
            make.right.equalTo(arrowImageView.snp.left).offset(-5)
        }
        

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func changeAddress() {
        let vc = MCChangeCommunityViewController()
        vc.model = self.model
        closure?(vc)
    }
    
    
    lazy var lineOneView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()

    
    lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.text = "配送社区"
        return label
    }()
    
    
    lazy var lineTwoView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()

    
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(15)
        return label
    }()

    
    lazy var addressButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Reuse_location"), for: .normal)
        button.titleLabel?.font = MCFont(13)
        button.setTitleColor(UIColor.MCWords, for: .normal)
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
        button.isEnabled = false
        button.isUserInteractionEnabled = false
        return button
    }()
    

    lazy var arrowImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_arrow")
        return imageView
    }()
    
    
}

